/*
 * Copyright (c) 2007 Aleksey Nikiforov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'Aleksey Nikiforov' nor the names of other contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.lex.input;

import org.lex.input.effects.Plane;

import com.jme.math.Vector2f;
import com.jme.math.Vector3f;

public class GeometricPlane implements Plane {

	private Vector3f worldTranslation = new Vector3f();
	
	protected Vector3f xVector;
	protected Vector3f yVector;
	protected Vector3f upVector;
	
	/** if you modify this variable, you must call updateWorldTranslation() */
	protected Vector2f location = new Vector2f();
	/** if you modify this variable, you must call updateWorldTranslation() */
	private float height;
	
	/** temp vector */
	private Vector3f temp = new Vector3f();
	
	public GeometricPlane() {
		setPlane(Vector3f.UNIT_X, Vector3f.UNIT_Z, Vector3f.UNIT_Y);
	}
	
	/**
	 * xVec and yVec can be any two orthonormal vectors.
	 * (These vectors have to be normalized and orthogonal to each other).
	 * 
	 * These vectors will define a scrolling plane, and the camera orientation
	 * will define the scrolling direction.
	 * 
	 * The up vector is required to define the height offset via setHeight().
	 * The height offset will affect the worldTranslation of the point on
	 * the scrolling plane.
	 * 
	 * @param xVec
	 * @param yVec
	 * @param up is needed to define the height offset
	 */
	public GeometricPlane(Vector3f xVec, Vector3f yVec, Vector3f up) {
		setPlane(xVec, yVec, up);
	}
	
	/**
	 * xVec and yVec can be any two orthonormal vectors.
	 * (These vectors have to be normalized and orthogonal to each other).
	 * 
	 * These vectors will define a scrolling plane, and the camera orientation
	 * will define the scrolling direction.
	 * 
	 * The up vector is required to define the height offset via setHeight().
	 * The height offset will affect the worldTranslation of the point on
	 * the scrolling plane.
	 * 
	 * @param xVec
	 * @param yVec
	 * @param up is needed to define the height offset
	 */
	public void setPlane(Vector3f xVector, Vector3f yVector, Vector3f up) {
		this.xVector = xVector;
		this.yVector = yVector;
		this.upVector = up;
	}
	
	public Vector3f getUpVector() {
		return upVector;
	}
	
	public Vector3f getWorldTranslation() {
		return worldTranslation;
	}
	
	public void setLocationFromWolrdTranslation(Vector3f worldTranslation) {
		location.x = xVector.dot(worldTranslation);
		location.y = yVector.dot(worldTranslation);
		updateWorldTranslation();
	}
	
	public void setLocationFromWolrdTranslation(float x, float y, float z) {
		worldTranslation.set(x, y, z);
		setLocationFromWolrdTranslation(worldTranslation);
	}
	
	public Vector2f getLocation() {
		return location;
	}
	
	public void setLocation(Vector2f location) {
		setLocation(location.x, location.y);
	}
	
	public void setLocation(float x, float y) {
		location.set(x, y);
		updateWorldTranslation();
	}
	
	public float getHeight() {
		return height;
	}
	
	public void setHeight(float height) {
		this.height = height;
		updateWorldTranslation();
	}
	
	protected void updateWorldTranslation() {
		worldTranslation.x =
			location.x*xVector.x + location.y*yVector.x + height*upVector.x;
		worldTranslation.y =
			location.x*xVector.y + location.y*yVector.y + height*upVector.y;
		worldTranslation.z =
			location.x*xVector.z + location.y*yVector.z + height*upVector.z;
	}
	
	/**
	 * @see org.lex.input.effects.Plane#findPlanarDistance(com.jme.math.Vector3f, com.jme.math.Vector3f)
	 */
	public float findPlanarDistance(Vector3f p1, Vector3f p2) {
		Vector3f direction = p1.subtract(p2, temp);
		temp.set(xVector.dot(direction), yVector.dot(direction), 0);
		return temp.length();
	}
	
	public Vector2f projectOntoPlane(Vector3f v, Vector2f result) {
		result.x = xVector.dot(v);
		result.y = yVector.dot(v);
		return result;
	}
	
	/**
	 * @see org.lex.input.effects.Plane#getHeightOf(com.jme.math.Vector3f)
	 */
	public float getHeightOf(Vector3f v) {
		return v.dot(upVector);
	}
	
	/**
	 * This method takes into account the height offset of the plane.
	 * For example: if the height offset is 10 and the height passed
	 * as an argument is 20, then the vector will have a height of 30.
	 * 
	 * @param v
	 * @param height
	 */
	public void setRelativeHeightOf(Vector3f v, float height) {
		float currentHeight = v.dot(upVector);
		float finalHeight = height + this.height;
		temp.set(upVector);
		temp.multLocal(finalHeight - currentHeight);
		v.addLocal(temp);
	}
	
	/**
	 * @see org.lex.input.effects.Plane#setHeightOf(com.jme.math.Vector3f, float)
	 */
	public void setHeightOf(Vector3f v, float height) {
		float currentHeight = v.dot(upVector);
		temp.set(upVector);
		temp.multLocal(height - currentHeight);
		v.addLocal(temp);
	}
}
